package com.huluobo.high3demo.startLive;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.ActivityStartLiveBinding;
import com.huluobo.mvvm.baen.PushBeanResult;
import com.huluobo.mvvm.baen.StopPushBeanResult;
import com.huluobo.mvvm.view.BaseMVVMActivity;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class StartLiveActivity extends BaseMVVMActivity<ActivityStartLiveBinding, StartLiveViewModel> {
    private V2TXLivePusher v2TXLivePusher;
//    private V2TXLivePlayer v2TXLivePlayer;


    private void stopPush() {
        // 结束推流
        v2TXLivePusher.stopPush();
//        v2TXLivePlayer.stopPlay();
        viewModel.stopPush();
    }

    private void startPush() {
        v2TXLivePusher.setRenderView(dataBinding.startTxPushView);
        v2TXLivePusher.startCamera(true);//开启摄像头预览
        v2TXLivePusher.startMicrophone();//开启麦克风
        // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
        String url = "rtmp://190253.push.tlivecloud.com/live/123?txSecret=d0f8b5ec7153c492bee71f9405035751&txTime" +
                "=6514E2B3";
        int ret = v2TXLivePusher.startPush(url);//开始推流
        if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
            Log.i("TagA", "startRTMPPush: license 校验失败");
        }

        //开播时,需要调用服务器接口,吧地址和直播描述传递给服务器
        viewModel.push(url, dataBinding.startEtCaption.getText().toString());
    }

    @Override
    protected void initData() {
        viewModel.pushSuccess.observe(this, new Observer<PushBeanResult>() {
            @Override
            public void onChanged(PushBeanResult pushBeanResult) {
                Log.i("TagA", "pushBeanResult:" + pushBeanResult.getPushUrl());
                Log.i("TagA", "pushBeanResult:" + pushBeanResult.getFlvUrl());

//                当成功推流产生返回的时候,会拿到播放地址,可以直接用腾讯云的播放器播放
//                int code = v2TXLivePlayer.startLivePlay(pushBeanResult.getFlvUrl());
//                Log.i("TagA", "code:" + code);
                dataBinding.startTxPullView.setUp(pushBeanResult.getFlvUrl(),false,"");
                dataBinding.startTxPullView.startPlayLogic();
            }
        });

        viewModel.stopPushSuccess.observe(this, new Observer<StopPushBeanResult>() {
            @Override
            public void onChanged(StopPushBeanResult stopPushBeanResult) {
                ToastUtils.showLong("下播成功");
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.startPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPush();
            }
        });

        dataBinding.startStopPush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopPush();
            }
        });

        // 指定对应的直播协议为 RTMP，该协议不支持连麦
        v2TXLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);

//        //初始化拉流的view
//        v2TXLivePlayer = new V2TXLivePlayerImpl(StartLiveActivity.this);
//        //关键 player 对象与界面 view
//        v2TXLivePlayer.setRenderView(dataBinding.startTxPullView);
    }

    @Override
    protected Class<StartLiveViewModel> getViewModel() {
        return StartLiveViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_live;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopPush();
    }
}